"use strict";
cc._RF.push(module, 'fca76bJZGhOwK1DJpAYYwNJ', 'game');
// scripts/game.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var fruit_1 = require("./fruit");
var NewClass = /** @class */ (function (_super) {
    __extends(NewClass, _super);
    function NewClass() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.game_root = null;
        _this.grade = null;
        // 水果集，预制体
        _this.fruits = [];
        // 当前新回合可以下落的水果
        _this.current_fruit = null;
        // 如果是新回合，则可以点击或滑动，如果不是，则不处理
        _this.is_new_round = false;
        // 能产生的最大的水果索引
        _this.random_max = 0;
        return _this;
    }
    NewClass.prototype.onLoad = function () {
        var _this = this;
        cc.director.getPhysicsManager().enabled = true;
        cc.director.getPhysicsManager().debugDrawFlags = 0;
        cc.director.getPhysicsManager().gravity = cc.v2(0, -320);
        cc.director.getCollisionManager().enabled = true;
        cc.director.getCollisionManager().enabledDebugDraw = true;
        this.game_root.on(cc.Node.EventType.TOUCH_START, function (e) {
            _this.choose_position(e);
        }, this);
        this.game_root.on(cc.Node.EventType.TOUCH_MOVE, this.choose_position, this);
        this.game_root.on(cc.Node.EventType.TOUCH_END || cc.Node.EventType.TOUCH_CANCEL, this.drop, this);
    };
    NewClass.prototype.start = function () {
        this.game_root.zIndex = 1;
        this.new_round();
    };
    NewClass.prototype.new_round = function () {
        var that = this;
        var fruit = cc.instantiate(this.fruits[Math.floor(Math.random() * this.random_max)]);
        fruit.setPosition(cc.v2(0, 400));
        this.game_root.addChild(fruit);
        fruit.on('sameContact', function (_a) {
            var self = _a.self, other = _a.other;
            // 两个node都会触发，临时处理，看看有没有其他方法只展示一次的
            other.node.off('sameContact');
            // 处理水果合并的逻辑，下面再处理
            that.onSameFruitContact(self, other);
        });
        this.current_fruit = fruit;
        // 表示新回合
        this.is_new_round = true;
    };
    // 水果下落
    NewClass.prototype.drop = function (e) {
        var _this = this;
        if (this.is_new_round) {
            this.current_fruit.getComponent(fruit_1.default).drop();
            this.is_new_round = false;
            setTimeout(function () {
                _this.new_round();
            }, 1500);
        }
    };
    // 鼠标按下时移动位置
    NewClass.prototype.choose_position = function (e) {
        if (this.current_fruit != null && this.is_new_round) {
            var postion = this.game_root.convertToWorldSpaceAR(this.current_fruit.position);
            postion.x = e.getLocationX();
            this.current_fruit.position = this.game_root.convertToNodeSpaceAR(postion);
        }
    };
    // 相同水果相撞
    NewClass.prototype.onSameFruitContact = function (self, other) {
        var that = this;
        // 获取水果名称中的索引
        var index = self.node.name.split("_")[1];
        index = Math.min(this.fruits.length - 1, index);
        //  销毁两个碰撞的水果
        self.node.removeFromParent(false);
        other.node.removeFromParent(false);
        // 产生新的水果
        var new_fruit = cc.instantiate(this.fruits[index]);
        new_fruit.setPosition(other.node.getPosition());
        this.game_root.addChild(new_fruit);
        // 让新水果下落到底部
        new_fruit.getComponent(fruit_1.default).drop();
        // 新水果绑定碰撞事件
        new_fruit.on('sameContact', function (_a) {
            var self = _a.self, other = _a.other;
            // 两个node都会触发，临时处理，看看有没有其他方法只展示一次的
            other.node.off('sameContact');
            // 处理水果合并的逻辑，下面再处理
            that.onSameFruitContact(self, other);
        });
        // 处理分数
        this.grade.string = Number(this.grade.string) + index;
        // 处理产生最大水果的索引
        this.random_max = Math.max(this.random_max, index);
        if (this.random_max > this.fruits.length) {
            this.random_max = this.fruits.length;
        }
    };
    __decorate([
        property(cc.Node)
    ], NewClass.prototype, "game_root", void 0);
    __decorate([
        property(cc.Label)
    ], NewClass.prototype, "grade", void 0);
    __decorate([
        property(cc.Prefab)
    ], NewClass.prototype, "fruits", void 0);
    NewClass = __decorate([
        ccclass
    ], NewClass);
    return NewClass;
}(cc.Component));
exports.default = NewClass;

cc._RF.pop();